package com.lagou.web.servlet;

import com.lagou.base.Constants;
import com.lagou.pojo.Course;
import com.lagou.service.Impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;
import com.lagou.utils.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
@WebServlet("/SaveCourse")
public class SaveCourseInfo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            Course course = new Course();
            HashMap<String, Object> map = new HashMap<>();
            //1、磁盘文件工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            //获取表单集合
            List<FileItem> list = upload.parseRequest(req);
            if (list!=null){
                for(FileItem item:list) {
                    boolean formField = item.isFormField();
                    if (formField){

                        //普通
                        //属性
                        String fieldName = item.getFieldName();
                        String value = item.getString("utf-8");
                        System.out.println(fieldName+"="+ value);
                        map.put(fieldName,value);

                    }else{

                        String name = item.getName();
                        String newFileName = UUIDUtils.getUUID()+"_"+name;

                       String realPath = this.getServletContext().getRealPath("/");

                        String path = realPath.substring(0, realPath.indexOf("test_war"));


                        InputStream in = item.getInputStream();
                        FileOutputStream out = new FileOutputStream(path+"/public"+newFileName);
                        IOUtils.copy(in,out);


                        map.put("course_img_Url", Constants.Local_Name+"/public"+newFileName);
                        in.close();
                        out.close();
                    }
                }

            }
            BeanUtils.populate(course,map);
            CourseServiceImpl cs = new CourseServiceImpl();
            String dateFormart = DateUtils.getDateFormart();

            if (map.get("id")!=null){
                course.setUpdate_time(dateFormart);
                String s = cs.SaveCourseSales(course);
                resp.getWriter().print(s);
            }else{
                course.setCreate_time(dateFormart);
                course.setUpdate_time(dateFormart);
                String s =cs.SaveCourseSales(course);
                resp.getWriter().print(s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
